package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateUserIndexIntro
//@description: 创建UserIndexIntro记录
//@param: userIndexIntro model.UserIndexIntro
//@return: err error

func CreateUserIndexIntro(userIndexIntro model.UserIndexIntro) (err error) {
	err = global.GVA_DB.Create(&userIndexIntro).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserIndexIntro
//@description: 删除UserIndexIntro记录
//@param: userIndexIntro model.UserIndexIntro
//@return: err error

func DeleteUserIndexIntro(userIndexIntro model.UserIndexIntro) (err error) {
	err = global.GVA_DB.Delete(&userIndexIntro).Error
	return err
}

func DeleteUserIndexIntroByUserId(UserId int)(err error) {
	err = global.GVA_DB.Delete(&[]model.UserIndexIntro{},"index_id = ?",UserId).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserIndexIntroByIds
//@description: 批量删除UserIndexIntro记录
//@param: ids request.IdsReq
//@return: err error

func DeleteUserIndexIntroByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserIndexIntro{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateUserIndexIntro
//@description: 更新UserIndexIntro记录
//@param: userIndexIntro *model.UserIndexIntro
//@return: err error

func UpdateUserIndexIntro(userIndexIntro model.UserIndexIntro) (err error) {
	err = global.GVA_DB.Save(&userIndexIntro).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserIndexIntro
//@description: 根据id获取UserIndexIntro记录
//@param: id uint
//@return: err error, userIndexIntro model.UserIndexIntro

func GetUserIndexIntro(id uint) (err error, userIndexIntro model.UserIndexIntro) {
	err = global.GVA_DB.Where("id = ?", id).First(&userIndexIntro).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserIndexIntroInfoList
//@description: 分页获取UserIndexIntro记录
//@param: info request.UserIndexIntroSearch
//@return: err error, list interface{}, total int64

func GetUserIndexIntroInfoList(info request.UserIndexIntroSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.UserIndexIntro{})
    var userIndexIntros []model.UserIndexIntro
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&userIndexIntros).Error
	return err, userIndexIntros, total
}